package org.itboys.basetemplate.web;

import org.itboys.admin.entity.BusinessCenterUser;
import org.itboys.admin.service.BusinessCenterUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.basetemplate.entity.Voucher;
import org.itboys.basetemplate.entity.VoucherRule;
import org.itboys.basetemplate.entity.VoucherUserMap;
import org.itboys.basetemplate.service.VoucherRuleService;
import org.itboys.basetemplate.service.VoucherService;
import org.itboys.basetemplate.service.VoucherUserMapService;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.member.entity.Member;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.utils.page.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 代金券规则管理
 * 
 * @author Administrator
 * 
 */

@RestController
@RequestMapping("/VoucherRuleController")
public class VoucherRuleController extends BaseController {

	@Autowired
	private VoucherRuleService voucherRuleService;
	@Autowired
	private VoucherService voucherService;
	@Autowired
	private VoucherUserMapService voucherUserMapService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private BusinessCenterUserService businessCenterUserService;

	@RequestMapping("/toVM")
	public ModelAndView listvm(HttpServletRequest request, HttpServletResponse response, Model model) {
		return new ModelAndView("/adve/voucherRule");
	}

	/**
	 * 分页查询
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/list")
	public void list(HttpServletRequest request, HttpServletResponse response) {
		BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("centerId", user.getCenterId());
		Page<VoucherRule> page = voucherRuleService.pageQuery(sqlMap, request);
		for(VoucherRule rule : page.getData()) {
			rule.setVoucherName(voucherService.getById(rule.getVoucherId()).getName());
		}
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	/**
	 * 新增修改时保存
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping("/save")
	public void save(@ModelAttribute VoucherRule voucher, HttpServletRequest request, HttpServletResponse response) {
		try {
			BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
			voucher.setCenterId(user.getCenterId());
			if(voucher.getId() != 0) {
				voucherRuleService.update(voucher);
			} else {
				voucherRuleService.save(voucher);
			}
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response, "fail");
		}
	}

	/**
	 * 根据Id查询
	 * 
	 * @param id
	 * @param request
	 * @param response
	 */
	@RequestMapping("/findById")
	public void findById(@RequestParam Long id, HttpServletRequest request, HttpServletResponse response) {
		VoucherRule voucher = voucherRuleService.getById(id);
		AjaxUtils.renderJson(response, voucher);
	}

	/**
	 * @param id
	 * @param response
	 */
	@RequestMapping("/doConfirm")
	public void doConfirm(@RequestParam Long id, @RequestParam Integer status, HttpServletResponse response) {
		try {
			VoucherRule voucher = voucherRuleService.getById(id);
			if(status == 0) {
				voucher.setStatus(1);
			} else if(status == 1) {
				voucher.setStatus(0);
			}
			voucherRuleService.update(voucher);
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			AjaxUtils.renderText(response, "fail");
		}
	}
	
	/**
	 * 分配折扣券
	 * @param id
	 * @param memberId
	 * @param response
	 */
	@RequestMapping("/give")
	public void give(@RequestParam Long id, @RequestParam Long memberId, HttpServletResponse response) {
		try {
			Member member = memberService.getById(memberId);
			if(member == null) {
				AjaxUtils.renderText(response, "-2");
				return;
			}
			VoucherRule rule = voucherRuleService.getById(id);
			Voucher voucher = voucherService.getById(rule.getVoucherId());
			VoucherUserMap userMap = voucherUserMapService.giveVoucher(memberId, voucher);
			if(userMap == null) {
				AjaxUtils.renderText(response, "-1");
			} else {
				AjaxUtils.renderText(response, "success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response, "fail");
		}
	}

	/**
	 * 分配折扣券(批量)
	 * @param id
	 * @param response
	 */
	@RequestMapping("/giveVoucher")
	public void giveVoucher(@RequestParam Long id, @RequestParam Long[] ids, HttpServletResponse response) {
		try {
			VoucherRule rule = voucherRuleService.getById(id);
			Voucher voucher = voucherService.getById(rule.getVoucherId());
			for(Long idLong : ids) {
				voucherUserMapService.giveVoucher(idLong, voucher);
			}
			AjaxUtils.renderText(response, "success");
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response, "fail");
		}
	}
	
	@RequestMapping("/getAll")
	public void getAll(HttpServletRequest request, HttpServletResponse response) {
		BusinessCenterUser user = businessCenterUserService.getById(AdminSessionHolder.getAdminUserId());
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("centerId", user.getCenterId());
		sqlMap.put("status", 1);
		List<VoucherRule> list = voucherRuleService.list(sqlMap);
		AjaxUtils.renderJson(response, list);
	}
}
